package org.jeecgframework.web.material.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.material.entity.ScheduleEntity;
import org.jeecgframework.web.material.service.ScheduleServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: 工作安排
 * @date 2015-08-23 13:02:07
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/scheduleController")
public class ScheduleController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ScheduleController.class);

	@Autowired
	private ScheduleServiceI scheduleService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "schedule")
	public ModelAndView schedule(HttpServletRequest request) {
		return new ModelAndView("material/schedule/scheduleList");
	}
	
	/**
	 * 工作安排新增或编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goAdd(ScheduleEntity schedule, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(schedule.getId())) {
			schedule = scheduleService.getEntity(ScheduleEntity.class, schedule.getId());
			req.setAttribute("schedulePage", schedule);
		}
		return new ModelAndView("material/schedule/schedule");
	}


	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ScheduleEntity schedule,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ScheduleEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, schedule, request.getParameterMap());
		try{
		//自定义追加查询条件
			TSUser user = ResourceUtil.getSessionUserName();
			cq.createAlias("companyId", "companyId");
			cq.eq("companyId.id", user.getCompany().getId());
			cq.addOrder("endTime", SortDirection.desc);//按结束日期降序排序
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.scheduleService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 添加工作安排
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAdd(ScheduleEntity schedule, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if(StringUtil.isEmpty(schedule.getId())){
			message = "工作安排添加成功";
			try{
				TSUser user = ResourceUtil.getSessionUserName();
				schedule.setCompanyId(user.getCompany());
				scheduleService.save(schedule);
				systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
			}catch(Exception e){
				e.printStackTrace();
				message = "工作安排添加失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}else{
			message = "工作安排更新成功";
			ScheduleEntity t = scheduleService.get(ScheduleEntity.class, schedule.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(schedule, t);
				scheduleService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "工作安排更新失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}
		
		return j;
	}
	

	/**
	 * 删除工作安排
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ScheduleEntity schedule, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		schedule = systemService.getEntity(ScheduleEntity.class, schedule.getId());
		message = "工作安排删除成功";
		try{
			scheduleService.delete(schedule);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "工作安排删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除工作安排
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "工作安排删除成功";
		try{
			for(String id:ids.split(",")){
				ScheduleEntity schedule = systemService.getEntity(ScheduleEntity.class, 
				id
				);
				scheduleService.delete(schedule);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "工作安排删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	 /**
		 * 当前用户工作页面跳转
		 * 
		 * @return
		 */
		@RequestMapping(params = "goWork")
		public ModelAndView goWork(HttpServletRequest req) {
			
			return new ModelAndView("material/schedule/workList");
		}
	
}
